package com.xuecheng.web.exception;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Sober_wxy
 * @date 2022/7/16 15:54
 */
@RestControllerAdvice(
        basePackages = {
                "com.xuecheng.system.controller",
                "com.xuecheng.content.controller",
                "com.xuecheng.learning.controller",
                "com.xuecheng.order.controller",
                "com.xuecheng.search.controller",
                "com.xuecheng.media.controller",
                "com.xuecheng.basic.controller",
                "com.xuecheng.teaching.controller"
        }
)
@Slf4j
public class GlobalExceptionHandler {
        /**
         * 处理全局异常
         * @param e 异常对象
         * @return 返回错误信息 错误码
         */
        @ExceptionHandler(value = RuntimeException.class)
        public ResponseResult handlerRuntimeException(RuntimeException e){
                log.error(e.getMessage(),e);
                return ResponseResult.errorResult(ErrorCode.ERROR);
        }

        /**
         * 处理自定义业务异常
         * @param e 异常对象
         * @return 返回错误信息 错误码
         */
        @ExceptionHandler(value = BusinessException.class)
        public ResponseResult handlerBusinessException(BusinessException e){
                ErrorCode errorCode = e.getErrorCode();
                return ResponseResult.errorResult(errorCode);
        }
}
